package org.example.io.util;

import io.netty.channel.Channel;
import io.netty.util.Attribute;
import org.example.io.session.ServerSession;
import org.example.io.protocol.Attributes;

public class LoginUtil {
    /**
     * 标记登录
     * @param channel
     */
    public static void markAsLogin(Channel channel) {
        channel.attr(Attributes.IS_LOGIN).set(true);
    }

    /**
     * 判断是否已经登录
     * @param channel
     * @return
     */
    public static boolean hasLogin(Channel channel) {
        Attribute attr = channel.attr(Attributes.IS_LOGIN);
        return attr.get() != null && attr.get().equals(true);
    }

    public static void saveSession(ServerSession session, Channel channel) {
        LoginUtil.markAsLogin(channel);
        channel.attr(Attributes.SERVER_SESSION).set(session);
        session.setChannel(channel);
    }
}
